<?php 
/*
 * Created on 2009-11-16
 *
 * Author: Sanmao
 */
require_once ("platform/Interceptor.php");
require_once (dirname(__FILE__)."/services.php");

class com_yizin_config_RouterExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/admin/site/config/$}'] = dirname(__FILE__)."/actions.php";
        $chain->next($context);
    }
}
class com_yizin_config_PrivilegeExtension extends Extension {

    public function apply(&$context, Chain $chain) {
        $context['items']['site']['text'] = "网站管理";
        $context['items']['site']['items']['config']['text'] = "配置";
        $chain->next($context);
    }
}
class com_yizin_config_AdminMenuExtension extends Extension {

    public function apply(&$context, Chain $chain) {
    
        global $currentUser,$featureManager;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
        
            $userContext = array("user"=>$currentUser['serial']);
            $featureManager->apply("com.yizin.platform.role.get.by.user", $userContext);
            $roleContext = array('role'=>$userContext['role']['serial'],
                                     'id'=>'site.config');
            $featureManager->apply("com.yizin.platform.privilege.get.by.id", $roleContext);
            if (($roleContext['allow'])) {
                $context['items']['site']['href'] = REQUEST_BASE."admin/site/config/";
                $context['items']['site']['text'] = "网站配置";
            }
        }
        
        $chain->next($context);
    }
}
class com_yizin_config_InterceptorExtension extends Extension {

    public function apply(&$context, Chain $chain) {
    
        $context['rule']['{^/admin/site/config/.*$}'][] = new com_yizin_config_LoginInterceptor();
        $context['rule']['{^/admin/site/config/.*$}'][] = new com_yizin_config_AdminInterceptor();
        
        $chain->next($context);
    }
}
class com_yizin_config_LoginInterceptor extends Interceptor {

    public function intercept(&$context, InterceptorChain $chain) {
    
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $chain->next($context);
        } else {
            $mvc = new MVC(array());
            $mvc->setMessage("message", "请先登录");
            $currentURI = Utils::getURI();
            $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
        }
    }
}

class com_yizin_config_AdminInterceptor extends Interceptor {

    public function intercept(&$context, InterceptorChain $chain) {
    
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
        
            $userContext = array("user"=>$currentUser['serial']);
            $featureManager->apply("com.yizin.platform.role.get.by.user", $userContext);
            $roleContext = array('role'=>$userContext['role']['serial'], 'id'=>'site.config');
            $featureManager->apply("com.yizin.platform.privilege.get.by.id", $roleContext);
            if (!($roleContext['allow'])) {
                $mvc = new MVC(array());
                $mvc->setMessage("message", "帐户权限不足");
                $currentURI = Utils::getURI();
                $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
            }
            ;
            
        }
        $chain->next($context);
    }
}
class com_yizin_config_GetConfigExtension extends Extension {
   public function apply(&$context,
                        Chain $chain){  
    $context['data'] = array();
	$context['data'] = site_config_getConfig();
    
    $chain->
            next($context);
    
}
}

class com_yizin_config_configFeature extends Feature {
    public function registerExtension() {
        $this->extensions = Array( 
              	 new com_yizin_config_RouterExtension("com.yizin.platform.router"),
                 new com_yizin_config_AdminMenuExtension("com.yizin.platform.admin.menu"),
                 new com_yizin_config_InterceptorExtension("com.yizin.platform.interceptor"),
                 new com_yizin_config_PrivilegeExtension("com.yizin.platform.privilege"),
				 new com_yizin_config_GetConfigExtension("com.yizin.site.get.name"));
    }

    
    public function registerExtendable() {
    }
    
    public function init() {
    	$conn = getDBConnection();
		$conn->query('drop table if exists t_site_config;');
    	$conn->query('
		create table t_site_config (
  		meta_key varchar(255) not null,
  		meta_value text,
  		primary key (meta_key)
		) charset = utf8;');
		$conn->query('insert into t_site_config (meta_key, meta_value) values ("site_name", "WDL")');
        $conn->query('insert into t_site_config (meta_key, meta_value) values ("copyright", "&copy; 2010 <strong>WDL CityPast</strong>")');
        $conn->query('insert into t_site_config (meta_key, meta_value) values ("tech_support", "WDL")');
		$conn->disconnect();
	}
    
    public function destroy() {
    	$conn=getDBConnection();
		$conn->query('drop table if exists t_site_config;');
		$conn->disconnect();
    }
}

$featureClass = "com_yizin_config_configFeature";
$featureName = "Site Config";
$featureIntroduction = "config feature";
?>
